﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Windows.Forms;
using Tesla.Core;

namespace Tesla.Windows.Util {

    /// <summary>
    /// Utility class for setting up an application message loop with a supplied user form and/or main loop that will be used
    /// for updating/rendering and is executed when the application has finished processing messages (e.g. is idle).
    /// </summary>
    public static class MessagePump {

        /// <summary>
        /// Sets up the heartbet to be executed during the application's idle state and calls
        /// Application.Run() using the supplied user form.
        /// </summary>
        /// <param name="form">Application's main form</param>
        /// <param name="mainLoop">Heartbeat update/render loop</param>
        public static void Run(Form form, Heartbeat mainLoop) {
            Application.Idle += new EventHandler(new IdleHandler(mainLoop).OnIdle);
            Application.Run(form);
        }

        /// <summary>
        /// Sets up the heartbeat to be executed during the application's idle state and calls
        /// Application.Run() using the supplied application context.
        /// </summary>
        /// <param name="context">Application thread context</param>
        /// <param name="mainLoop">Heartbeat update/render loop</param>
        public static void Run(ApplicationContext context, Heartbeat mainLoop) {
            Application.Idle += new EventHandler(new IdleHandler(mainLoop).OnIdle);
            Application.Run(context);
        }

        /// <summary>
        /// Sets up the heartbeat to be executed during the application's idle state and
        /// aclls Application.Run().
        /// </summary>
        /// <param name="mainLoop">Heartbeat update/render loop</param>
        public static void Run(Heartbeat mainLoop) {
            Application.Idle += new EventHandler(new IdleHandler(mainLoop).OnIdle);
            Application.Run();
        }

        private class IdleHandler {
            private Heartbeat _loop;

            public IdleHandler(Heartbeat loop) {
                _loop = loop;
            }

            public void OnIdle(Object sender, EventArgs args) {
                Tesla.Windows.Util.Message msg;
                while(!NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0)) {
                    _loop();
                }
            }
        }
    }
}
